<#include "../include/header.html">
<script type="text/javascript">
    var viewModel = Hap.createGridViewModel("#Grid");
    viewModel.model.set("tokenStatus","valid");
</script>
<body>

    <div id="page-content">
        <div class="pull-right" id="query-form" style="padding-bottom:10px;">
            <div style="float:left;margin-right:5px;">
                <input id="userId" type="text" style="width:150px;" placeholder='<@spring.message "user.userName"/>'
                       data-bind="value:model.userId" >
                <script>
                    $("#userId").kendoLov($.extend(${lovProvider.getLov(base.contextPath, base.locale, "user_lov")}, {}))
                </script>
            </div>
            <input type="text" data-role="maskedtextbox" style="float:left;width:150px;margin-right:5px;" placeholder='<@spring.message "oauth2clientdetails.clientid"/>' data-bind="value:model.clientId" class="k-textbox">
            <input id="priority" data-bind="value:model.tokenStatus" style="float:left;width:150px;margin-right:5px;" placeholder='<@spring.message "tokenlogs.tokenaccessstatus"/>'/>
            <script type="text/javascript">
                $("#priority").kendoDropDownList({
                    dataTextField: "label",
                    dataValueField: "status",
                    template: function(data){
                        return data.label
                    },
                    dataSource: [
                        { label: '<@spring.message "tokenlogs.tokenaccessall"/>',status:'all' },
                        { label: '<@spring.message "tokenlogs.tokenaccessvalid"/>',status:'valid'},
                        { label: '<@spring.message "tokenlogs.tokenaccessinvalid"/>',status:'invalid'},
                    ]
                });
            </script>
            <input type="text" style="float:left;width:150px;margin-right:5px;" data-bind="value:model.token" class="k-textbox" placeholder='token'/>

            <span class="btn btn-primary" style="float:left;width:70px;" data-bind="click:query" type="submit"><i class="fa fa-search" style="margin-right:3px;"></i><@spring.message "hap.query"/></span>

            <div style="clear:both"></div>
        </div>
        <script>kendo.bind($('#query-form'), viewModel);</script>
        <div style="clear:both">
            <div id="Grid"  style="clear: both" ></div>
        </div>
    </div>

<script type="text/javascript">

    kendo.bind($('#page-content'), viewModel);

    Hap.initEnterQuery('#query-form', viewModel.query);

    var crudServiceBaseUrl = '${base.contextPath}/sys/token/logs/',
        dataSource = Hap.createGridDataSource({
            url: crudServiceBaseUrl,
            model: {
                id: "id",
                fields: {}
            },
            sort:{field: 'tokenAccessTime', dir: 'desc'}
        })

    $("#Grid").kendoGrid({
        dataSource: dataSource, 
        sortable: true,
        resizable: true,
        scrollable: true,
        navigatable: false,
        dataBound: function(){
                   if(parent.autoResizeIframe){
                       parent.autoResizeIframe('${RequestParameters.functionCode!}')
                   }
               },
        pageable: {
            pageSizes: [5, 10, 20, 50],
            refresh: true,
            buttonCount: 5
        },
        columns: [
           
            {
                field: "userId",
                title: '<@spring.message "tokenlogs.userid"/>',
                width: 50
            },
            {
                field: "clientId",
                title: '<@spring.message "oauth2clientdetails.clientid"/>',
                width: 80
            },
                    {
                field: "token",
                title: 'token',
                width: 160
            },
            {
                field: "tokenAccessTime",
                title: '<@spring.message "tokenlogs.tokenaccesstime"/>',
                headerAttributes: {
                      style: "text-align: center"
                },
                attributes: {style: "text-align:center"},
                width: 80
            },
            {
                field: "tokenExpiresTime",
                title: '<@spring.message "tokenlogs.tokenexpirestime"/>',
                headerAttributes: {
                style: "text-align: center"
                },
                attributes: {style: "text-align:center"},
                width: 80
            },
            {
                field: "tokenAccessType",
                title: '<@spring.message "tokenlogs.tokenaccesstype"/>',
                width: 80
            },
            {
                title: '<@spring.message "tokenlogs.tokenaccessstatus"/>',
                width: 60,
                headerAttributes: {
                    style: "text-align: center"
                },
                attributes: {
                    style: "text-align: center"
                },
                template: function (dataItem) {
                    
                    if(dataItem.tokenStatus == "valid"){
                        return "<span style='color:green;'><@spring.message "tokenlogs.tokenaccessvalid"/></span>";
                    }           
                     return "<span style='color:#5d6d7c;'><@spring.message "tokenlogs.tokenaccessinvalid"/></span>";
                 
                },
            },
            {
                title: '<@spring.message "tokenlogs.tokenaccessaction"/>',
                headerAttributes: {
                    style: "text-align: center"
                },
                attributes: {
                    style: "text-align: center"
                },
                width: 60,
                template: function (dataItem) {
                     if(dataItem.tokenStatus == "valid"){
                        var div = "<div>";
                        div += Hap.createAnchor("<span style='margin-right:5px'><@spring.message "tokenlogs.tokenaccessrevoke"/></span>", revokeToken, dataItem.token);
                        div += "</div>"
                        return div;
                    }else{
                        return "";
                    }
                },
                sortable: false
            },
        ],
        editable: false
    });

    function revokeToken(token){
         kendo.ui.showConfirmDialog({
            title: '<@spring.message "hap.prompt"/>',
            message: '<@spring.message "tokenlogs.cancelauthorization"/>'
        }).done(function (e) {
            if(e.button == "OK") {
                $.ajax({
                    type: 'POST',
                    url: "${base.contextPath}/sys/token/logs/revoke",
                    data: "token=" + token,
                    success: function (data) {
                        $('#Grid').data('kendoGrid').dataSource.page(1);
                    }
                });
            }
        });
    }




</script>
</body>
</html>